'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 购买商品
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var moment = require('moment');
var dataApi = require('../../../../libs/dataApi');
var rewardType = require('../../../../consts/rewardType');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var PlayerManager = require('../../../../libs/playerManager');
var vipConf = require('../../../../config/vipConf');
var vipShopConf = require('../../../../config/vipShopConf');
var rewardType = require('../../../../consts/rewardType');
var rewardService = require('../../../../services/rewardService');
var roleService = require('../../../../services/roleService');
var taskService = require('../../../../services/taskService');
var vipConf = require('../../../../config/vipConf.js');
var constantUtil = require('../../../../util/constantUtil.js');

module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var shop_id = msg.shop_id;
    var has_buy_num;
    if (!shop_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var reward_list = [];
    var player_manager = new PlayerManager(role_id);
    async.waterfall([

            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'bag', 'vip', 'wishtree', 'build', 'soldier', 'shop', 'tech', 'accrued', 'discount'],
                    cb
                );
            },
            function (cb) {
                var role_model = player_manager.role;
                var vip_model = player_manager.vip;
                var discount_model = player_manager.discount;

                // 是否开放
                var open_time = constantUtil.value('discount_shop_open_time');
                var end_time = constantUtil.value('discount_shop_finish_time');
                var now_time = moment().unix();
                if (open_time > now_time && end_time < now_time) {
                    return next(null, {
                        code: Code.DISCOUT_SHOP_OPEN_TIME_NOT
                    });
                }

                reward_list = dataApi.discount_shop.findById(shop_id);
                if (reward_list.length <= 0) {
                    return next(null, {
                        code: Code.FAIL
                    });
                }

                // 是否还有次数
                var now_buy_num = discount_model.getDiscountShopNum(shop_id);
                if (now_buy_num >= reward_list[0].buy_times) {
                    return next(null, {
                        code: Code.DISCOUT_SHOP_BUY_NUM_NOT
                    });
                }
                var sub_diamond = reward_list[0].discount_price;

                // 扣除钻石
                if (!roleService.subDiamond(player_manager, moduleType.MDL_ACT_DISCOUNT_SHOP, operationType.OPR_ACT_DISCOUNT_SHOP_BUY, sub_diamond, shop_id)) {
                    return next(null, {
                        code: Code.USER_DIAMOND_NOT_ENOUGH
                    });
                }

                // 记录次数
                discount_model.addDiscountShopNum(shop_id);

                // 发放资源奖励
                rewardService.addRewardListToRole(player_manager, reward_list, moduleType.MDL_ACT_DISCOUNT_SHOP, operationType.OPR_ACT_DISCOUNT_SHOP_BUY);
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    reward_list: reward_list
                }
            });
        }
    );
};
